﻿Ext.define('app.prm.project_task.ChooseQuan',
{
    extend: 'Ext.window.Window',
    autoScroll: true,
    modal: true,
    title: '选中成员',
    width: 800,
    height: 600,
    layout: 'fit',
    bodyStyle: 'padding:2px',
    projectTaskId: 0,
    projectId: 0,
    initComponent: function () {
        var me = this;
        me.items = [me.getQuanPanel()];
        me.buttons = [
            {
                text: '确定', handler: function () {
                    me.onSave();
                }
            },
            { text: '关闭', handler: function () { me.close(); } }
        ];
        me.callParent();
    },
    onSave: function () {
        var me = this;
        var checkeds = me.getTaskMember().getChecked();
        var members = [];
        var memberIds = [];
        for (var i = 0; i < checkeds.length; i++) {
            var node = checkeds[i];
            if (parseInt(node.data.id) > 0) {
                memberIds.push(node.data.id);
                members.push(node.data.text);
            }
        }

        var quans = [];
        var quanIds = [];
        var quanCheckeds = me.getQuanTree().getChecked();
        for (var i = 0; i < quanCheckeds.length; i++) {
            var node = quanCheckeds[i];
            if (me.nodep(node)) {
                continue;
            }
            if (parseInt(node.data.id) > 0) {
                quanIds.push({ key: node.data.id, value: node.data.parentId });
                quans.push(node.data.text);
            }
        }

        me.fireEvent("savesuccess", memberIds, members.toString(), quans.toString(), quanIds);

    },
    getQuanPanel: function () {
        var me = this;
        if (!this.quanPanel) {
            this.quanPanel = new Ext.tab.Panel({
                items: [
                    {
                        title: '项目成员',
                        layout: 'fit',
                        items: [
                           me.getTaskMember()
                        ]
                    },
                    {
                        title: '区域',
                        items: [
                            me.getQuanTree()
                        ]
                    }
                ]
            });
        }
        return me.quanPanel;
    },
    getTaskMember: function () {
        var me = this;
        if (!this.taskMember) {
            this.taskMember = new Ext.tree.Panel({
                border: true,
                useArrows: false,
                rootVisible: false,
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: false,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/projectTask/GetRoleUserTree?projectId=' + me.projectId + "&taskId=" + me.projectTaskId
                    }
                }),
                root: { id: '0', text: '' },
                listeners: {
                    afterrender: function (tree) {
                        tree.expandAll();
                    },
                    checkchange: function (node, checked, eOpts) {
                        if (checked) {
                            node.eachChild(function (child) {
                                me.checkChild(child, true);
                            });
                        } else {
                            node.eachChild(function (child) {
                                me.checkChild(child, false);
                            });
                        }
                        me.checkParent(node);// 进行父级选中操作
                    }
                }
            });
        }
        return this.taskMember;
    },
    getQuanTree: function () {
        var me = this;
        if (!this.tree) {

            this.tree = new Ext.tree.Panel({
                border: false,
                tbar: [
                    {
                        text: '载入成员区域', handler: function () {
                            var checkeds = me.getTaskMember().getChecked();

                            var array = [];
                            for (var i = 0; i < checkeds.length; i++) {
                                var node = checkeds[i];
                                if (parseInt(node.data.id) > 0) {
                                    array.push(node.data.id);
                                }
                            }

                            me.tree.store.load({
                                params: {
                                    projectTaskId: me.projectTaskId, users: array.toString()
                                },
                                callback: function () {
                                    me.tree.expandAll();

                                    var rootNode = me.tree.getRootNode();
                                    me.checkUserQuan(rootNode);
                                }
                            });
                        }
                    }
                ],
                useArrows: true,
                rootVisible: false,
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/quan/GetTaskQuan?projectTaskId=' + me.projectTaskId
                    }
                }),
                root: { id: '0', text: '' },
                listeners: {
                    afterrender: function (tree) { tree.expandAll(); },
                    checkchange: function (node, checked, eOpts) {
                        if (checked) {
                            node.eachChild(function (child) {
                                me.checkChild(child, true);
                            });
                        } else {
                            node.eachChild(function (child) {
                                me.checkChild(child, false);
                            });
                        }
                        me.checkParent(node);// 进行父级选中操作
                    }
                }
            });
        }
        return this.tree;
    },
    checkUserQuan: function (node) {
        var me = this;
        var check = node.data.isUser;
        node.set('checked', check);
        if (node.isNode) {
            node.eachChild(function (child) {
                me.checkUserQuan(child, check);
            });
        }
        if (check)
        {
        me.checkParent(node);
        }
    },
    checkChild: function (node, check) {
        var me = this;
        node.set('checked', false);
        if (node.isNode) {
            node.eachChild(function (child) {
                me.checkChild(child, false);
            });
        }
    },
    nodep: function (node) {
        //有任意一个子节点选中的话，返回True
        var bnode = false;
        Ext.Array.each(node.childNodes, function (v) {
            if (v.data.checked) {
                bnode = true;
                return;
            }
        });
        return bnode;
    },
    checkParent: function (node) {
        var me = this;
        if (node.parentNode != null) {

            if (node.data.checked) {
                node.parentNode.set('checked', true);
            }
            else {
                if (me.nodep(node.parentNode)) {
                    return;
                } else {
                    node.parentNode.set('checked', false);
                }
            }
            me.checkParent(node.parentNode);
        }
    }
});